package com.wy.netty.tcp;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 
 *
 * @author 飞花梦影
 * @date 2024-04-28 17:27:32
 * @git {@link https://github.com/dreamFlyingFlower}
 */
public class ThreadFactoryImpl implements ThreadFactory {

	private final AtomicLong threadIndex = new AtomicLong(0);

	private final String threadNamePrefix;

	private final boolean daemon;

	public ThreadFactoryImpl(final String threadNamePrefix) {
		this(threadNamePrefix, false);
	}

	public ThreadFactoryImpl(final String threadNamePrefix, boolean daemon) {
		this.threadNamePrefix = threadNamePrefix;
		this.daemon = daemon;
	}

	@Override
	public Thread newThread(Runnable r) {
		Thread thread = new Thread(r, threadNamePrefix + this.threadIndex.incrementAndGet());
		thread.setDaemon(daemon);
		return thread;
	}
}